package com.micro.service.auth.server.config.oauth2;

import com.alibaba.fastjson.JSON;

import com.micro.service.auth.server.exception.AccountCenterException;
import com.micro.service.auth.server.exception.PasswordErrorException;
import com.micro.service.auth.server.exception.ValidateCodeException;
import com.micro.service.auth.server.utils.PropertyFilterUtil;
import com.micro.service.common.base.ResultBase;
import com.micro.service.common.enums.AuthorizationServerEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;


/**
 * @author lv
 * @Description: 自定义异常处理器
 * @create 2018-01-31 9:50
 * @last modify by [lv 2018-01-31 9:50]
 **/
public class CustomerWebResponseExceptionTranslator extends DefaultWebResponseExceptionTranslator {
	private final Logger log = LoggerFactory.getLogger(getClass());
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public ResponseEntity translate(Exception e) throws Exception {
		ResultBase resultBase = null;
		HttpHeaders headers = new HttpHeaders();
		ResponseEntity<OAuth2Exception> responseEntity = super.translate(e);
		if(e instanceof UsernameNotFoundException){
			log.error("CustomerWebResponseExceptionTranslator,用户没有找到:"+e);
            resultBase = new ResultBase(AuthorizationServerEnum.UserNotExistedException);
        }else if(e instanceof ValidateCodeException){
        	log.error("CustomerWebResponseExceptionTranslator,CaptchaException:"+e);
            resultBase = new ResultBase(AuthorizationServerEnum.ValidateCodeException);
        }else if(e instanceof PasswordErrorException){
        	log.error("CustomerWebResponseExceptionTranslator,密码错误:"+e);
            PasswordErrorException passwordErrorException = (PasswordErrorException) e;
            String message = passwordErrorException.getMessage();
            resultBase = new ResultBase(AuthorizationServerEnum.UserNameOrPasswordException);
        }else if(e instanceof InternalAuthenticationServiceException) {
        	if (e.getCause() instanceof AccountCenterException) {
        		AccountCenterException ex = (AccountCenterException) e.getCause();
            	log.error("CustomerWebResponseExceptionTranslator,账户中心异常:"+ex);
            	resultBase = new ResultBase(ex.getMessage(),ex.getCode());
			}else{
				log.error("CustomerWebResponseExceptionTranslator,InternalAuthenticationServiceException:非自定义异常:"+e);
	        	return responseEntity;
			}
		}else if(e instanceof InvalidTokenException){
			log.error("CustomerWebResponseExceptionTranslator,token异常:"+e);
			resultBase = new ResultBase(AuthorizationServerEnum.InvalidTokenException);
		}else if(e instanceof OAuth2Exception){
			log.error("CustomerWebResponseExceptionTranslator, OAuth验证异常:"+e);
			resultBase = new ResultBase(AuthorizationServerEnum.RefAccessTokenFail);
		}else{
        	log.error("CustomerWebResponseExceptionTranslator,非自定义异常:"+e);
        	return responseEntity;
        }
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.setAll(responseEntity.getHeaders().toSingleValueMap());
		return new ResponseEntity<Object>(JSON.toJSONString(resultBase, PropertyFilterUtil.commonFilter()), headers, HttpStatus.OK);
	}
}
